/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgUtil;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Administrador
 */
public class BuilderReport {

    private JRDataSource dataSource;
    private JasperReport jasperReport;
    private JasperPrint jasperPrint;
    private Map hmParameters;
    private Collection collection;
    private String pathFileJasper;
    private String nameReport;

    public BuilderReport() {
        hmParameters = new HashMap();
        collection = new LinkedList();
        pathFileJasper = new String();
        nameReport = new String();
    }

    public JRDataSource getDataSource() {
        return dataSource;
    }

    public JasperReport getJasperReport() {
        return jasperReport;
    }

    public JasperPrint getJasperPrint() {
        return jasperPrint;
    }

    public Map getHmParameters() {
        return hmParameters;
    }

    public void setHmParameters(Map hmParameters) {
        this.hmParameters = hmParameters;
    }

    public Collection getCollectionDetail() {
        return collection;
    }

    public void setCollectionDetail(Collection coleccion) {
        this.collection = coleccion;
    }

    public String getPathFileJasper() {
        return pathFileJasper;
    }

    public void setPathFileJasper(String pathFileJasper) {
        this.pathFileJasper = pathFileJasper;
    }

    public String getNameReport() {
        return nameReport;
    }

    public void setNameReport(String nameReport) {
        this.nameReport = nameReport;
    }

    public void addParameter(String key, Object value) {
        hmParameters.put(key, value);
    }

    public void deleteParameter(String key) {
        hmParameters.remove(key);
    }

    public Object getParameter(String key) {
        return hmParameters.get(key);
    }

    public void clearParameters() {
        hmParameters.clear();
    }

    public void addBeanToCollectionDetail(Object bean) {
        collection.add(bean);
    }

    public boolean deleteBeanFromCollectionDetail(Object bean) {
        return collection.remove(bean);
    }

    public void clearCollectionDetail() {
        collection.clear();
    }

    /*
     * public JasperReport loadReport() throws JRException { File archivo = new
     * File(pathFileJasper); jasperReport = (JasperReport)
     * JRLoader.loadObject(archivo); return jasperReport; }
     */
    public JasperReport loadReport() throws JRException {
        try {
            InputStream is = getClass().getResourceAsStream(pathFileJasper);
            jasperReport = (JasperReport) JRLoader.loadObject(is);
            return jasperReport;
        } catch (Exception ex) {
            throw new JRException(ex.getMessage());
        }
    }

    public void bindDataSource() {
        dataSource = new JRBeanCollectionDataSource(collection);
    }

    public JasperPrint buildReport() throws JRException {
        loadReport();
        jasperPrint = JasperFillManager.fillReport(
                jasperReport, hmParameters, dataSource);
        return jasperPrint;
    }

    public void showReport() {
        JasperViewer.viewReport(jasperPrint, false);
    }

    public void buildAndShowReport() throws JRException {
        this.bindDataSource();
        this.buildReport();
        this.showReport();
    }

    public void exportReportToPDF(String pathArchivoDestino) throws JRException {
        JasperExportManager.exportReportToPdfFile(
                jasperPrint, pathArchivoDestino);
    }
}
